package com.example.web.demos;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author scout.du
 * @date 2024/07/03 14:40
 **/
public class RestTemplateTestCase1 {


    /*
     * header不符合 报错content type类型不支持
     * header-->"content-type":"text/plain"
     * spring报错 content type类型不支持
     * system exception:Content type 'text/plain;charset=UTF-8' not supported
     *
     * */
    @Test
    public void test3(){

        RestTemplate restTemplate = new RestTemplate();

        String url = "https://app.dev.aws.gravity.porsche.cn/management-portal-dev-api/user/getUserInfo";

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.put("token", List.of("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJiZTZiMmI3OWZhYzQ0YjE1YTZhNjkzNTJjZmVmZjE1MCIsInN1YiI6IjMiLCJpc3MiOiJtYW5hZ2VtZW50LXBvcnRhbC5ncmF2aXR5LnBvcnNjaGUuY24iLCJhdWQiOiJtYW5hZ2VtZW50LXBvcnRhbC5ncmF2aXR5LnBvcnNjaGUuY24iLCJpYXQiOjE3MjM3OTExNTcsImV4cCI6MTcyMzk2Mzk1NywidXNlcm5hbWUiOiJkYnVzZXJAZGJhdXRoLmNvbSIsInJvbGVzIjpbIkFkbWluIl0sInBlcm1pc3Npb25zIjpbIkRBU0hCT0FSRF9WSUVXIiwiQVBQX09WRVJWSUVXIiwiQVBQX0RFVEFJTF9WSUVXIiwiQVBQX0RFVEFJTF9MSVNUX1ZJRVciLCJQT0RfTE9HX1ZJRVciLCJSRVNPVVJDRV9ERVRBSUxfVklFVyIsIlBPRF9FRElUIiwiU0VSVklDRV9FRElUIiwiREVQTE9ZTUVOVF9FRElUIiwiQ09ORklHTUFQX0VESVQiLCJTRUNSRVRfRURJVCIsIkFQUF9NT05JVFJPSU5HIiwiQVBQX01PTklUT1JJTkdfVklFVyIsIkFQUF9MT0dfQ0VOVEVSX1ZJRVciLCJBVURJVCIsIk9QRVJBVEVfSElTVE9SWSIsIlBSRUZFUkVOQ0UiLCJNRVNTQUdFX0NFTlRFUiIsIlFVT1RBX0VESVQiLCJBRE1JTl9DT05TT0xFIiwiQVBQX0FETUlOIiwiVEFHX0FETUlOIiwiUk9MRV9BRE1JTiIsIlVTRVJfQURNSU4iLCJGRUVEQkFDS19ISVNUT1JZIiwiRkVFREJBQ0tfQ0VOVEVSIiwiQ0RfVklFVyIsIkNEX0VESVQiLCJJTkdSRVNTX0VESVQiLCJDRF9TRVRUSU5HUyIsIkNPU1RfRVhQX1ZJRVciLCJDT1NUX01PREVMX1ZJRVciLCJMT0dfQ09ORklHIiwiQ09TVF9GSU5BTkNFIl19.WnwFwgeeEeAxHAWhJyAtMmghrkWBGYGYgDYRDKAFMWk"));
        headers.put("Content-Type", List.of("text/plain"));

        String requestBody = "123456";


        RequestEntity requestEntity = new RequestEntity<>(requestBody,headers,HttpMethod.POST,URI.create(url));
        ResponseEntity<String> entity = restTemplate.exchange(requestEntity, String.class);
        String response = entity.getBody();
        System.out.println(response);
    }

    /*
    * header符合 body不符合
    * header-->"content-type":"application/json"
    * body--> 非json格式
    * spring报错 json解析错误
    * system exception:JSON parse error: Cannot construct instance of `com.porsche.gravity.consolebff.domain.param.UserInfoQueryDto` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (123456); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.porsche.gravity.consolebff.domain.param.UserInfoQueryDto` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (123456)\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]
    *
    * */
    @Test
    public void test2(){

        RestTemplate restTemplate = new RestTemplate();

        String url = "https://app.dev.aws.gravity.porsche.cn/management-portal-dev-api/user/getUserInfo";

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.put("token", List.of("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJiZTZiMmI3OWZhYzQ0YjE1YTZhNjkzNTJjZmVmZjE1MCIsInN1YiI6IjMiLCJpc3MiOiJtYW5hZ2VtZW50LXBvcnRhbC5ncmF2aXR5LnBvcnNjaGUuY24iLCJhdWQiOiJtYW5hZ2VtZW50LXBvcnRhbC5ncmF2aXR5LnBvcnNjaGUuY24iLCJpYXQiOjE3MjM3OTExNTcsImV4cCI6MTcyMzk2Mzk1NywidXNlcm5hbWUiOiJkYnVzZXJAZGJhdXRoLmNvbSIsInJvbGVzIjpbIkFkbWluIl0sInBlcm1pc3Npb25zIjpbIkRBU0hCT0FSRF9WSUVXIiwiQVBQX09WRVJWSUVXIiwiQVBQX0RFVEFJTF9WSUVXIiwiQVBQX0RFVEFJTF9MSVNUX1ZJRVciLCJQT0RfTE9HX1ZJRVciLCJSRVNPVVJDRV9ERVRBSUxfVklFVyIsIlBPRF9FRElUIiwiU0VSVklDRV9FRElUIiwiREVQTE9ZTUVOVF9FRElUIiwiQ09ORklHTUFQX0VESVQiLCJTRUNSRVRfRURJVCIsIkFQUF9NT05JVFJPSU5HIiwiQVBQX01PTklUT1JJTkdfVklFVyIsIkFQUF9MT0dfQ0VOVEVSX1ZJRVciLCJBVURJVCIsIk9QRVJBVEVfSElTVE9SWSIsIlBSRUZFUkVOQ0UiLCJNRVNTQUdFX0NFTlRFUiIsIlFVT1RBX0VESVQiLCJBRE1JTl9DT05TT0xFIiwiQVBQX0FETUlOIiwiVEFHX0FETUlOIiwiUk9MRV9BRE1JTiIsIlVTRVJfQURNSU4iLCJGRUVEQkFDS19ISVNUT1JZIiwiRkVFREJBQ0tfQ0VOVEVSIiwiQ0RfVklFVyIsIkNEX0VESVQiLCJJTkdSRVNTX0VESVQiLCJDRF9TRVRUSU5HUyIsIkNPU1RfRVhQX1ZJRVciLCJDT1NUX01PREVMX1ZJRVciLCJMT0dfQ09ORklHIiwiQ09TVF9GSU5BTkNFIl19.WnwFwgeeEeAxHAWhJyAtMmghrkWBGYGYgDYRDKAFMWk"));
        headers.put("Content-Type", List.of("application/json"));

        String requestBody = "123456";


        RequestEntity requestEntity = new RequestEntity<>(requestBody,headers,HttpMethod.POST,URI.create(url),Map.class);
        ResponseEntity<String> entity = restTemplate.exchange(requestEntity, String.class);
        String response = entity.getBody();
        System.out.println(response);
    }


    //header和body一致  success
    @Test
    public void test1(){

        RestTemplate restTemplate = new RestTemplate();

        String url = "https://app.dev.aws.gravity.porsche.cn/management-portal-dev-api/user/getUserInfo";

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.put("token", List.of("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJiZTZiMmI3OWZhYzQ0YjE1YTZhNjkzNTJjZmVmZjE1MCIsInN1YiI6IjMiLCJpc3MiOiJtYW5hZ2VtZW50LXBvcnRhbC5ncmF2aXR5LnBvcnNjaGUuY24iLCJhdWQiOiJtYW5hZ2VtZW50LXBvcnRhbC5ncmF2aXR5LnBvcnNjaGUuY24iLCJpYXQiOjE3MjM3OTExNTcsImV4cCI6MTcyMzk2Mzk1NywidXNlcm5hbWUiOiJkYnVzZXJAZGJhdXRoLmNvbSIsInJvbGVzIjpbIkFkbWluIl0sInBlcm1pc3Npb25zIjpbIkRBU0hCT0FSRF9WSUVXIiwiQVBQX09WRVJWSUVXIiwiQVBQX0RFVEFJTF9WSUVXIiwiQVBQX0RFVEFJTF9MSVNUX1ZJRVciLCJQT0RfTE9HX1ZJRVciLCJSRVNPVVJDRV9ERVRBSUxfVklFVyIsIlBPRF9FRElUIiwiU0VSVklDRV9FRElUIiwiREVQTE9ZTUVOVF9FRElUIiwiQ09ORklHTUFQX0VESVQiLCJTRUNSRVRfRURJVCIsIkFQUF9NT05JVFJPSU5HIiwiQVBQX01PTklUT1JJTkdfVklFVyIsIkFQUF9MT0dfQ0VOVEVSX1ZJRVciLCJBVURJVCIsIk9QRVJBVEVfSElTVE9SWSIsIlBSRUZFUkVOQ0UiLCJNRVNTQUdFX0NFTlRFUiIsIlFVT1RBX0VESVQiLCJBRE1JTl9DT05TT0xFIiwiQVBQX0FETUlOIiwiVEFHX0FETUlOIiwiUk9MRV9BRE1JTiIsIlVTRVJfQURNSU4iLCJGRUVEQkFDS19ISVNUT1JZIiwiRkVFREJBQ0tfQ0VOVEVSIiwiQ0RfVklFVyIsIkNEX0VESVQiLCJJTkdSRVNTX0VESVQiLCJDRF9TRVRUSU5HUyIsIkNPU1RfRVhQX1ZJRVciLCJDT1NUX01PREVMX1ZJRVciLCJMT0dfQ09ORklHIiwiQ09TVF9GSU5BTkNFIl19.WnwFwgeeEeAxHAWhJyAtMmghrkWBGYGYgDYRDKAFMWk"));
        headers.put("Content-Type", List.of("application/json"));

        String requestBody = "{\"pageNum\":1,\"pageSize\":6,\"searchKey\":\"\",\"tagIds\":[]}";


        RequestEntity requestEntity = new RequestEntity<>(requestBody,headers,HttpMethod.POST,URI.create(url));
        ResponseEntity<String> entity = restTemplate.exchange(requestEntity, String.class);
        String response = entity.getBody();
        System.out.println(response);
    }




}
